package servlet;

import constant.ExamConstant;
import model.Doctor;
import model.RegRecord;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import service.RegRecordService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/export")
public class ExportServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        try {
            // 获取当前登录医生
            Object type = request.getSession().getAttribute(ExamConstant.SESSION_LOGIN_TYPE);
            if (!ExamConstant.LOGIN_TYPE_DOCTOR.equals(type)) {
                response.sendRedirect(ExamConstant.HOME_PAGE_LOGIN);
                return;
            }
            
            Doctor doctor = (Doctor) request.getSession().getAttribute(ExamConstant.SESSION_LOGIN_USER);
            if (doctor == null) {
                response.sendRedirect(ExamConstant.HOME_PAGE_LOGIN);
                return;
            }

            // 直接获取当前查询结果
            List<RegRecord> records = RegRecordService.query(null, doctor.getDeptId(), null, null);
            
            if (records == null || records.isEmpty()) {
                response.sendRedirect(ExamConstant.HOME_PAGE_DOCTOR + "?error=no_data");
                return;
            }

            // 创建工作簿
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("挂号记录");
            
            // 创建标题行
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("编号");
            headerRow.createCell(1).setCellValue("患者姓名");
            headerRow.createCell(2).setCellValue("就诊科室");
            headerRow.createCell(3).setCellValue("挂号费用");
            headerRow.createCell(4).setCellValue("挂号时间");
            
            // 填充数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            int rowNum = 1;
            for (RegRecord record : records) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(record.getId());
                row.createCell(1).setCellValue(record.getPatientName());
                row.createCell(2).setCellValue(record.getDeptName());
                row.createCell(3).setCellValue(record.getPrice().toString());
                row.createCell(4).setCellValue(sdf.format(record.getRegTime()));
            }
            
            // 自动调整列宽
            for (int i = 0; i < 5; i++) {
                sheet.autoSizeColumn(i);
            }
            
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=registration_records.xlsx");
            
            // 输出文件
            workbook.write(response.getOutputStream());
            workbook.close();
            
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect(ExamConstant.HOME_PAGE_DOCTOR + "?error=export_failed");
        }
    }
}